package com.zeei.gis.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeei.gis.dao.TradeCodeGisDao;
import com.zeei.gis.service.TradeCodeGisService;
import com.zeei.gis.vo.TradeCodeGisVO;

/**
 * @类型名称：TradeCodeGisServiceImpl
 * @类型描述：行业信息查询
 * @功能描述：行业信息查询
 * @创建人：jiangqingpeng
 */
@Service("tradeCodeGisService")
public class TradeCodeGisServiceImpl implements TradeCodeGisService{

	@Autowired
	TradeCodeGisDao tradeCodeGisDao;
	
	/**
	 * queryTradeCodeList:查询行业信息
	 */
	@Override
	public List<TradeCodeGisVO> queryTradeCodeList() {
		List<TradeCodeGisVO> tradeCodeList = tradeCodeGisDao.queryTradeCodeList();
		List<TradeCodeGisVO> rootList = new ArrayList<TradeCodeGisVO>();	//一级行业list
		if (tradeCodeList != null && !tradeCodeList.isEmpty()) {
			for (TradeCodeGisVO tradeCodeGisVO : tradeCodeList) {
				if (StringUtils.isEmpty(tradeCodeGisVO.getPcode())) {
					rootList.add(tradeCodeGisVO);	//得到全部一级行业
				}
			}
			
		}
		List<TradeCodeGisVO> childList = null;	//二级行业
		for (int i = 0; i < rootList.size(); i++) {
			childList = new ArrayList<>();
			for (TradeCodeGisVO tradeCodeGisVO : tradeCodeList) {
				if (rootList.get(i).getCodeTrade().equals(tradeCodeGisVO.getPcode()) && (tradeCodeGisVO.getLevel() == 2)) {
					childList.add(tradeCodeGisVO);	//得到每个一级行业的全部二级行业
				}
				rootList.get(i).setChildren(childList);		//将对应的二级行业设置为一级行业的子
			}
		}
		return rootList;
	}

}
